﻿using UnityEngine;
using System.Collections;

public class TroubleCoin : Actors
{
	public float m_SpeedToIcon = 10.0f;

	private bool m_HasCollided = false;
	private int m_CoinValue = 1;
	private EmptySquare m_OverSquare;
	
	public int CoinValue {get { return m_CoinValue; }}
	public EmptySquare OverSquare {get { return m_OverSquare; } set { m_OverSquare = value;}}

	void Awake () 
	{
		float randomCoin = Random.Range (0f, 1f);

		if(randomCoin <= 0.6f)
		{
			m_CoinValue = 1;
			GetComponent<SpriteRenderer>().color = new Color(1f, 0.5f, 0f);
		}
		else if(randomCoin > 0.6f && randomCoin <= 0.9f)
		{
			m_CoinValue = 2;
		}
		else
		{
			m_CoinValue = 3;
			GetComponent<SpriteRenderer>().color = new Color(1f, 1f, 0f);
		}
	}

	void Update () 
	{
		if(m_HasCollided)
		{
			aTransform.position = Vector3.MoveTowards(	aTransform.position, 
			                                          	(GamesManager.Instance.CurrentGame as TroubleGame).IconScore[(GamesManager.Instance.CurrentGame as TroubleGame).CurrentTurn].transform.position,
			                                          	m_SpeedToIcon * Time.deltaTime);
			if(aTransform.position.y == (GamesManager.Instance.CurrentGame as TroubleGame).IconScore[(GamesManager.Instance.CurrentGame as TroubleGame).CurrentTurn].transform.position.y)
			{
				(GamesManager.Instance.CurrentGame as TroubleGame).Players[(GamesManager.Instance.CurrentGame as TroubleGame).CurrentTurn].CurrentPoints += m_CoinValue;
				(GamesManager.Instance.CurrentGame as TroubleGame).UpdateScoreText();
				Destroy(aGameObject);
			}
		}
	}

	private void OnTriggerEnter2D(Collider2D other)
	{
		if(!m_HasCollided && other.tag == "Player")
		{
			m_HasCollided = true;
			if(!m_OverSquare.PlayerOver)
			{
				m_OverSquare.SquareValue = 0;
			}
			(GamesManager.Instance.CurrentGame as TroubleGame).CreateCoins();
		}
	}

}
